<template>
  <div id="background_image">
    <el-container>
      <el-header>系统登录</el-header>
      <div id="main">
        <el-row>
          <!--登录框居中-->
          <el-col :span="6" :offset="9">
            <div id="input">
              <el-row :gutter="10">
                <el-col>
                  <el-input
                    placeholder="请输入用户名"
                    v-model="username"
                    :maxlength="20"
                    clearable
                    prefix-icon="el-icon-s-custom"
                  ></el-input>
                </el-col>
              </el-row>
              <br />
              <el-row :gutter="10">
                <el-col>
                  <el-input
                    placeholder="请输入密码"
                    v-model="password"
                    show-password
                    prefix-icon="el-icon-lock"
                  ></el-input>
                </el-col>
              </el-row>
              <br />
              <el-button type="primary" @click="LoginFunc(username, password)"
                >登录</el-button
              >
            </div>
          </el-col>
        </el-row>
      </div>
    </el-container>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs";
import { mapState } from "vuex";
export default {
  name: "login",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  mounted() {
    // 当组件挂载的时候就发请求获取mock/user.json中已有的用户数据
    // this.$store.dispatch("getUserLogin");
  },
  methods: {
    LoginFunc(username, password) {
      var data = { username, password };
      //  向服务器发请求验证用户登录信息
      this.$axios
        .post("/api/login", qs.stringify(data), {
          headers: {'Content-Type':'application/x-www-form-urlencoded'},
        })
        .then((res) => {
          if(res.status==200){//当登录成功时
            this.$alert("登录成功",{
              confirmButtonText:'确定',
              callback:()=>{
                this.$router.push("/admin")
              }
            })
          }
        });
    },
    /*  LoginFunc(username, password) {
      // 验证用户的登录信息与仓库中保存的用户信息是否匹配
      this.userLogin.map((ele) => {
        if (!username) this.$alert("请输入用户名");
        else if (!password) this.$alert("密码错误");
        else if (ele.userName != username)
          this.$alert("用户名错误，请重新输入");
        else if (ele.password != password) this.$alert("密码错误，请重新输入");
        else if (ele.userName == username && ele.password == password) {
          this.$alert("登录成功", {
            confirmButtonText: "确定",
            callback: () => {
              this.$router.push("/admin"); //登录成功之后跳到首页
            },
          });
        }
      });
    }, */
  },
  computed: {
    ...mapState({
      userLogin: (state) => state.user.userLogin,
    }),
  },
};
</script>

<style scoped>
/*加入背景样式*/
@import "../../assets/static/css/background_image.css";
.el-header {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}
#input {
  height: 200px;
  margin-top: 300px;
}
</style>
